﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Windows.Forms;

namespace StudentApartMentManage
{
    public partial class AddStuInfo : Form
    {
        public AddStuInfo()
        {
            InitializeComponent();
        }
        string GetRstr(string sql)
        {
            string str = null;
            string connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open();
            SqlDataReader r = cmd.ExecuteReader();
            if (r.Read())
            {
                str = r.GetString(0);
            }
            r.Close();
            con.Close();
            return str;
        }
        void show(string sql)
        {
            dataGridView1.DataSource = sqlHelper.GetTable(sql);
        }
        private void StuManage_Load(object sender, EventArgs e)
        {
            string sql0 = "select 学号,姓名,性别,学院,专业,班级,民族 from 学生信息表";
            show(sql0);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要添加此学生的信息吗", "提示",MessageBoxButtons.YesNo) == DialogResult.Yes)
            { 
                string sql0 = string.Format("insert into 学生信息表(学号,姓名,性别,学院,专业,班级,民族) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", textBox1.Text.Trim(), textBox2.Text.Trim(), textBox3.Text.Trim(), textBox4.Text.Trim(), textBox5.Text.Trim(), textBox6.Text.Trim(), textBox7.Text.Trim());
                int i = sqlHelper.Execute(sql0);
                if (i != -1)
                {
                    MessageBox.Show("添加学生信息成功!");
                    show("select 学号,姓名,性别,学院,专业,班级,民族 from 学生信息表");
                }
                else
                    MessageBox.Show("添加学生信息失败!");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要重置学生的信息吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                textBox5.Clear();
                textBox6.Clear();
                textBox7.Clear();
            }
        }
    }
}
